<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <AddDoctorDrawer></AddDoctorDrawer>
    <UpdateDoctorDrawer ref="updateDoctor"></UpdateDoctorDrawer>
    <a-table :columns="columns" :data-source="doctorData" :pagination="{pageSize:7}">
      <a slot="name" slot-scope="text">{{ text }}</a>
      <span slot="customTitle">
        <a-icon type="smile-o"/>
        姓名
      </span>
      <span slot="type" slot-scope="type">
        <a-tag :color="'green'">
<!--          通过typeid从后台得到typename-->
          {{ getDoctorType(type)['typeName'] }}
        </a-tag>
      </span>
      <span slot="gender" slot-scope="gender">
        <a-tag
            :color="gender === 1 ? 'geekblue' : 'red'"
        >
          {{ gender === 1 ? '男' : '女' }}
        </a-tag>
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="sendOneDoctor(record.key)">Update</a>
        <a-divider type="vertical"/>
        <a @click="deleteOneDoctor(record.key)">Delete</a>
        <a-divider type="vertical"/>
        <a class="ant-dropdown-link"> 查看往期病历 <a-icon type="down"/> </a>
      </span>
    </a-table>
  </div>
</template>

<script>
import axios from "axios";
import columns from './AdminDoctor.js'
import AddDoctorDrawer from "@/components/drawer/AddDoctorDrawer.vue"
import UpdateDoctorDrawer from "@/components/drawer/UpdateDoctorDrawer.vue";
import {mapGetters} from "vuex";
import Breadcrumb from "@/components/Breadcrumb.vue"

export default {
  name: "AdminDoctor",
  components: {AddDoctorDrawer, UpdateDoctorDrawer,Breadcrumb},
  props:[''],
  data() {
    return {
      doctorData: [],
      columns,
      //医生所属科室数组
      types: []
    }
  },
  computed: {
    ...mapGetters(['getSign'])
  },
  methods: {
    getAdminDoctor() {
      axios.get("/api/adminDoctor/show")
          .then(response => {
            this.doctorData = response.data
            console.log("data : ", response.data);
          })
          .catch(error => {
            console.log(error);
          });
    },
    //TODO:完成！
    deleteOneDoctor(key) {
      axios({
        url: '/api/adminDoctor/deleteOne/' + key,
        method: 'post'
      }).catch(res => {
        console.log('删除doctor出错')
        console.log(res)
      })
      this.$nextTick(() => {
        this.getAdminDoctor();
      })
    },
    //更新病人信息,发送数据
    sendOneDoctor(key) {
      this.$refs.updateDoctor.showUpdate();
      axios({
        url: `/api/adminDoctor/getOneDoctor/${key}`,
        method: 'get'
      }).then((res) => {
        //获取数据
        console.log('res.data = ', res.data)
        this.$bus.$emit('sendDoctorData', res.data)
        this.$bus.$emit('showDoctorUpdate')
      }).catch(res => {
        console.log("取出病人数据出错", res)
      })
    },
    //获取医生具体科室名称
    getDoctorType(typeId) {
      console.log('getDoctorType', this.types[typeId - 1])
      return this.types[typeId - 1]
      // return this.types.indexOf(typeId).typeName
    },
  },
  watch: {
    //监听sign值的变化
    getSign() {
      this.$nextTick(() => {
        this.getAdminDoctor()

      })
    }

  },
  //提供病人基本数据
  mounted() {
    // let that = this;
    this.getAdminDoctor();
    axios.get("/api/adminDoctor/getDoctorTypes")
        .then(res => {
          this.types = res.data
          console.log('医生所属科室', res.data)
        })
        .catch(err => {
          console.log('医生所属科室报错', err);
        })
    //检查用户是否登录
    if (this.$session.get('token') == null) {
      this.$router.push("/login")
    }
  },
}
</script>

<style scoped>

</style>